package com.msblearn.C_005;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * T10_ReentrantLock:可重入锁
 *
 * 还可以指定为公平锁
 */
public class T01_ReentrantLock5 extends Thread {
    //设置为true表示为公平所。
    private  static ReentrantLock lock = new ReentrantLock(true);

    @Override
    public void run() {
        for(int i=0;i<100;i++){
            try {
                lock.lock();
                System.out.println(Thread.currentThread().getName()+"获得锁");
            }finally {
                lock.unlock();
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        T01_ReentrantLock5 reentrantLock5 = new T01_ReentrantLock5();
        Thread td = new Thread(reentrantLock5);
        Thread td2 = new Thread(reentrantLock5);
        td.start();
        td2.start();

    }
}
